#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
Simplified BSD License (BSD)
Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 06-Nov-09 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;

using Thn;
#endregion

namespace Thn.Interface.Vcl
{
    /// <summary>
    /// A control that toggles among various states.
    /// </summary>
    [ToolboxItem(true), DefaultProperty("Text"), DefaultEvent("CheckedChanged"), ToolboxBitmap(typeof(ResourceBeacon), "Thn.Interface.Vcl.Resources.Icons.CheckBox.bmp")]
#if DEBUG
    public
#else
    public
#endif
 class CheckBox : ControlBase
    {
        #region Fields
        #endregion

        #region Properties

        #region Checked
        private bool mChecked;
        /// <summary>
        /// Gets/Sets whether this control is selected.
        /// </summary>
        [Browsable(true), DefaultValue(false), Category("Appearance"), Description("Whether this control is selected")]
        public bool Checked
        {
            get { return mChecked; }
            set
            {
                bool previous = mChecked;
                mChecked = value;
                Invalidate();
                OnCheckedChanged(previous, mChecked);
                RaiseCheckedChanged(previous, mChecked);
                RaiseValueChanged(EventArgs.Empty);
            }
        }
        #endregion

        #region Alignment
        private ContentAlignment mAlignment = ContentAlignment.MiddleLeft;
        /// <summary>
        /// Gets or Sets alignment of content.
        /// </summary>
        [Browsable(true), DefaultValue(ContentAlignment.MiddleLeft), Category("Appearance"), Description("How to arrange contents")]
        public ContentAlignment Alignment
        {
            get { return mAlignment; }
            set
            {
                if (mAlignment != value)
                {
                    mAlignment = value;
                    InvalidateLayout();
                }
            }
        }
        #endregion

        #region Multiline
        private bool mMultiline = true;
        /// <summary>
        /// Gets/Sets whether long text shall be rendered as multiple rows.
        /// </summary>
        [Browsable(true), DefaultValue(true), Category("Appearance"), Description("Whether long text shall be rendered as multiple rows")]
        public bool Multiline
        {
            get { return mMultiline; }
            set
            {
                mMultiline = value;
                Invalidate();
            }
        }
        #endregion

        #region Text Style
        private TextStyle mTextStyle = TextStyle.Normal;
        /// <summary>
        /// Gets/Sets font style for rendering text.
        /// </summary>
        [Browsable(true), DefaultValue(TextStyle.Normal), Category("Appearance"), Description("Font style for rendering text")]
        public TextStyle TextStyle
        {
            get { return mTextStyle; }
            set
            {
                mTextStyle = value;
                Invalidate();
            }
        }
        #endregion

        #region Render Context
        private CheckBoxRenderContext mRenderContext = new CheckBoxRenderContext();
        /// <summary>
        /// Gets reference to current renderation context.
        /// </summary>
        protected CheckBoxRenderContext RenderContext
        {
            get { return mRenderContext; }
        }
        #endregion

        #endregion

        #region Events

        #region Checked Changed
        /// <summary>
        /// This event is raised after property Checked is changed.
        /// </summary>
        public event BooleanValueChangedHandler CheckedChanged;

        /// <summary>
        /// Invoke event CheckedChanged
        /// </summary>
        protected void RaiseCheckedChanged(bool oldValue, bool newValue)
        {
            BooleanValueChangedHandler handler = CheckedChanged;
            if (handler != null) handler(this, oldValue, newValue);
        }
        #endregion

        #region Value Changed
        /// <summary>
        /// This event is raised after property Checked is changed.
        /// </summary>
        public event EventHandler ValueChanged;

        /// <summary>
        /// Invoke event ValueChanged
        /// </summary>
        protected void RaiseValueChanged(EventArgs args)
        {
            EventHandler handler = ValueChanged;
            if (handler != null) handler(this, args);
        }
        #endregion

        #endregion

        #region Set Checked
        /// <summary>
        /// Set value for property Checked and invalidate without causing event CheckedChanged to be raised.
        /// </summary>
        /// <param name="value">New value for Checked.</param>
        public void SetChecked(bool value)
        {
            bool old = mChecked;
            mChecked = value;
            OnCheckedChanged(old, value);
            Invalidate();
        }
        #endregion

        #region On Checked Changed
        /// <summary>
        /// This method is invoked when property Checked is modified.
        /// </summary>
        protected virtual void OnCheckedChanged(bool oldValue, bool newValue) { }
        #endregion

        #region On Get Render Context
        /// <summary>
        /// Get reference to the renderation context which is used by <see cref="IControlRenderer"/>.
        /// </summary>
        protected override RenderContext OnGetRenderContext(Graphics canvas)
        {
            return mRenderContext;
        }
        #endregion

        #region On Calculate Layout
        /// <summary>
        /// Perform layout calculations neccessary for this control.
        /// </summary>
        protected override void OnCalculateLayout()
        {
            Rectangle rControl = this.ClientRectangle;
            int pad = 2;
            int h = rControl.Height - pad * 2;
            int w = rControl.Width - pad * 3 - h;
            int mx = (rControl.Left + rControl.Right) / 2;
            int my = (rControl.Top + rControl.Bottom) / 2;
            Rectangle rIcon = Rectangle.Empty;
            Rectangle rText = Rectangle.Empty;

            switch (mAlignment)
            {
                #region Left
                case ContentAlignment.TopLeft:
                    rIcon = new Rectangle(pad, pad, h, h);
                    rText = new Rectangle(rIcon.Right + pad, pad, w, h);
                    break;
                case ContentAlignment.MiddleLeft:
                    rIcon = new Rectangle(pad, pad, h, h);
                    rText = new Rectangle(rIcon.Right + pad, pad, w, h);
                    break;
                case ContentAlignment.BottomLeft:
                    rIcon = new Rectangle(pad, pad, h, h);
                    rText = new Rectangle(rIcon.Right + pad, pad, w, h);
                    break;
                #endregion

                #region Center
                case ContentAlignment.TopCenter:
                    w = rControl.Width - pad * 2;
                    rIcon = new Rectangle(mx - h / 2, my - h / 2, h, h);
                    rText = new Rectangle(mx - w / 2, my - h / 2, w, h);
                    break;
                case ContentAlignment.MiddleCenter:
                    w = rControl.Width - pad * 2;
                    rIcon = new Rectangle(mx - h / 2, my - h / 2, h, h);
                    rText = new Rectangle(mx - w / 2, my - h / 2, w, h);
                    break;
                case ContentAlignment.BottomCenter:
                    w = rControl.Width - pad * 2;
                    rIcon = new Rectangle(mx - h / 2, my - h / 2, h, h);
                    rText = new Rectangle(mx - w / 2, my - h / 2, w, h);
                    break;
                #endregion

                #region Right
                case ContentAlignment.TopRight:
                    rText = new Rectangle(pad, pad, w, h);
                    rIcon = new Rectangle(rText.Right + pad, pad, h, h);
                    break;
                case ContentAlignment.MiddleRight:
                    rText = new Rectangle(pad, pad, w, h);
                    rIcon = new Rectangle(rText.Right + pad, pad, h, h);
                    break;
                case ContentAlignment.BottomRight:
                    rText = new Rectangle(pad, pad, w, h);
                    rIcon = new Rectangle(rText.Right + pad, pad, h, h);
                    break;
                #endregion

                default:
                    throw new NotSupportedException("Unexpected alignment value");
                    break;
            }

            mRenderContext.IconRegion = rIcon;
            mRenderContext.TextRegion = rText;
        }
        #endregion

        #region On Click
        protected override void OnClick(EventArgs e)
        {
            if (Enabled)
            {
                Checked = !Checked;
                
                //set focus to this control if not focused yet
                if (!Focused) Focus();
                #region Defensive tracing
                else
                {
                    //already focused
                }
                #endregion
            }
            #region Defensive tracing
            else
            {
                //not enabled
            }
            #endregion
        }
        #endregion

        #region On Key Up
        protected override void OnKeyUp(KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.Space) || (e.KeyCode == Keys.Enter))
            {
                OnClick(EventArgs.Empty);
            }
            #region Defensive tracing
            else
            {
                //not interested in other keys
            }
            #endregion
        }
        #endregion

        #region On Mouse Enter
        protected override void OnMouseEnter(EventArgs e)
        {
            mRenderContext.IsMouseOver = true;
            Invalidate();
        }
        #endregion

        #region On Mouse Leave
        protected override void OnMouseLeave(EventArgs e)
        {
            mRenderContext.IsMouseOver = false;
            Invalidate();
        }
        #endregion

        #region On Lost Focus
        protected override void OnLostFocus(EventArgs e)
        {
            mRenderContext.IsMouseOver = false;
            base.OnLostFocus(e);
            Invalidate();
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor.
        /// </summary>
        public CheckBox()
        {
            //mTransparencyMethod = TransparencyMethod.ForcedPainting;
            this.Size = new Size(100, 22);
        }
        #endregion
    }

    //public delegate void BooleanValueChangeEventHandler(object 
}
